import { _decorator, Component, Node, Label, Toggle, Input, EventTouch } from 'cc';
import CCUtils from '../util/CCUtils';
import { EventMgr } from './EventManager';
import { GameEvent } from './GameConst';
import { LayerItem } from './LayerItem';
import { PosInfo } from './PosInfo';
const { ccclass, property } = _decorator;

@ccclass('LayerTabItem')
export class LayerTabItem extends Component {

    private chooseEff:Node = null;
    private chooseNode:Node = null;
    public toggle:Toggle = null;
    private infoText:Label = null;
    private closeBtn:Node = null;
    private bgShowBtn:Node = null;

    public layerId:number = 1;
    public layer:LayerItem = null;//绑定的层

    onLoad(){
        this.chooseEff = CCUtils.findChild(this.node,"chooseEff");
        this.chooseNode = CCUtils.findChild(this.node,"chooseNode");
        this.toggle = CCUtils.getToggle(this.node,"toggle");
        this.infoText = CCUtils.getLabel(this.node,"infoText");
        this.closeBtn = CCUtils.findChild(this.node,"closeBtn");
        this.bgShowBtn = CCUtils.findChild(this.node,"bgShowBtn");

        this.toggle.node.on('toggle', this.toggleChange, this);

        this.closeBtn.on(Input.EventType.TOUCH_END, this.onClickcloseBtn, this);
        this.bgShowBtn.on(Input.EventType.TOUCH_END, this.onClickBgShowBtn, this);
        this.chooseNode.on(Input.EventType.TOUCH_START, this.onClickChooseNode, this);
    }


    public init(layerId:number, layer:LayerItem) {
        this.closeBtn.active = true;
        this.toggle.isChecked = true;
        this.layerId = layerId;
        this.layer = layer;
        this.infoText.string = this.layerId.toString();

        this.toggleChange(this.toggle);
    }

    private toggleChange(toggle: Toggle){
        // this.closeBtn.active = this.toggle.isChecked;
        this.layer.node.active = this.toggle.isChecked;

        this.setChoose(this.toggle.isChecked);

        if (this.toggle.isChecked) {
            EventMgr.dispatchEvent(GameEvent.selectLayer, this);
        }

    }

    public setChoose(boo:boolean){
        this.chooseEff.active = boo;
    }

    private onClickcloseBtn(e:EventTouch) {
        EventMgr.dispatchEvent(GameEvent.delectLayer, this);
    }

    private onClickBgShowBtn(e:EventTouch) {
        if (this.layer && this.layer.bg) {
            this.layer.bg.node.active = !this.layer.bg.node.active
        }
    }

    private onClickChooseNode(e:EventTouch) {
        if (this.toggle.isChecked) {
            this.setChoose(this.toggle.isChecked);
            EventMgr.dispatchEvent(GameEvent.selectLayer, this);
        }
    }
}


